/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.jsf.components;

import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import obai.control.PlataformBean.Plataform;
import obai.model.util.PathParser;

/**
 *
 * @author Fernando Arena Varella
 * @version 1.0
 */
public class VideoComponent extends PravBaseComponent {
    
    public static final String COMPONENT_FAMILY = "PRAV_VIDEO";
    public static final String COMPONENT_TYPE = "PRAV_VIDEO";

    //attributes
    private String autoplay = "false", height, posterHref, width;
    private String mobileHref, tvdHref, webHref;

    public VideoComponent(){

    }
    
    @Override
    public void encodeBegin(FacesContext context) throws IOException {
        super.setDisplayMobile(false);
        super.setDisplayTv(false);
        super.setDisplayWeb(false);

		ResponseWriter writer =
			context.getResponseWriter();

        //if mobileHref is set and targetPlataform is MOBILE
        if ((plataformBean.getPlataform() == Plataform.MOBILE) &&
                (mobileHref != null)){
            buildMobileVideo(writer);
        }

        if ((plataformBean.getPlataform() == Plataform.DIGITALTV) &&
                (tvdHref != null))
           buildTvVideo(writer);

        if ((plataformBean.getPlataform() == Plataform.WEB) &&
                (webHref != null))
            buildWebVideo(writer);


        //fix src attribute


//        hidePlataformSpecificContent(writer);
        writer.flush();
	}

    @Override
    public void encodeEnd(FacesContext context)
            throws IOException {
//        if (!displayPlataform())
//            return;
        ResponseWriter writer = context.getResponseWriter();
//        writer.endElement("img");
        writer.flush();
    }

    @Override
    public void decode(FacesContext context) {
        return;
    }


    //util methods
    private void buildMobileVideo(ResponseWriter writer) throws IOException{
        writer.startElement("a", this);
        writer.writeAttribute("href", mobileHref, "href");
        if (title != null)
            writer.write(title);
        else
            writer.write("Video");
        writer.endElement("a");
    }
    
    private void buildTvVideo(ResponseWriter writer){
    }

    private void buildWebVideo(ResponseWriter writer) throws IOException{
        String CRLF = "\n\t\t\t";

//        src=\"/AC_QuickTime.js\" 

        StringBuilder sb = new StringBuilder();
        sb.append("<script src=\"AC_Quicktime.js\" language=\"JavaScript\" type=\"text/javascript\"></script>\n\t\t");
        sb.append("<script language=\"JavaScript\" type=\"text/javascript\">\n\t\t");
        sb.append("QT_WriteOBJECT(");

        boolean pathContainsProtocol = PathParser.pathContainsProtocol(webHref);

        if (!pathContainsProtocol)
            sb.append(posterHref != null ? "'" + posterHref + "'," : "'',");
        else
            sb.append("'',");

        sb.append(width != null ? "'" + width + "'," : "'',");
        //append height + 'ActiveX Version' + newLine and tabs
        sb.append(height != null ? "'" + height + "',''," + CRLF : "'',''," + CRLF);
        
        if (pathContainsProtocol){
            sb.append("'qtsrc','" + webHref + "'," + CRLF);
            sb.append("'TYPE','video/quicktime'," + CRLF);
            sb.append("'controller','true'," + CRLF);
        }
        //apend autoplay + );
        if (autoplay.equalsIgnoreCase("true"))
            sb.append("''autoplay','true');");
        else
            sb.append("'autoplay','false');");

        sb.append("\n\t\t</script>");

        writer.write(sb.toString());
    }


    // <editor-fold defaultstate="collapsed" desc="Getters And Setters">
    public String getAutoplay() {
        return autoplay;
    }

    public void setAutoplay(String autoplay) {
        this.autoplay = autoplay;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getPosterHref() {
        return posterHref;
    }

    public void setPosterHref(String posterHref) {
        this.posterHref = posterHref;
    }

    public String getMobileHref() {
        return mobileHref;
    }

    public void setMobileHref(String mobileHref) {
        this.mobileHref = mobileHref;
    }

    public String getTvdHref() {
        return tvdHref;
    }

    public void setTvdHref(String tvdHref) {
        this.tvdHref = tvdHref;
    }

    public String getWebHref() {
        return webHref;
    }

    public void setWebHref(String webHref) {
        this.webHref = webHref;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }
    // </editor-fold>

}
